<?php

class Request extends Controller {

    public $error;
    public $requestments;
    public $userLog;

    public function __construct() {
        parent::__construct();

        $this->_initialize();
    }

    public function __destruct() {
        parent::__destruct();
    }

    public function index() {
        if (isset($this->_session['user'])) {
            $this->userLog = $this->_session['user']['info'];
        } else {
            header('Location:index.php?c=login');
        }

        $this->template('request');
    }

    private function _initialize() {
        if (isset($this->_session['user'])) {
            $this->userLog = $this->_session['user']['info'];
        } else {
            header('Location:index.php?c=login');
        }

        $this->model('friend');
        $this->requestments = $this->friend->getRequest($this->userLog->id);
    }

    public function accept() {
        if (isset($this->_post['request'])) {
            //Check csrf security...
            $token = $this->_post['request']['token'];
            if ($token == $this->_token) {
                unset($this->_post['request']['token']);
                $idFriend = $this->_post['request']['idFriend'];
                //Get num view photo....
                //Load model friend..
                $this->model('friend');
                $result = $this->friend->accept($this->userLog->id, $idFriend);

                if ((bool) $result) {
                    echo "{result:1, id:'{$idFriend}', idself:{$this->userLog->id}}";
                } else {
                    echo "{result:1, id:'{$idFriend}', idself:{$this->userLog->id}}";
                }
            }
        }
    }

    public function deny() {
        if (isset($this->_post['request'])) {
            //Check csrf security...
            $token = $this->_post['request']['token'];
            if ($token == $this->_token) {
                unset($this->_post['request']['token']);
                $idFriend = $this->_post['request']['idFriend'];
                //Load model friend..
                $this->model('friend');
                $result = $this->friend->deny($this->userLog->id, $idFriend);

                if ((bool) $result) {
                    echo "{result:1, id:'{$idFriend}', idself:{$this->userLog->id}}";
                } else {
                    echo "{result:1, id:'{$idFriend}', idself:{$this->userLog->id}}";
                }
            }
        }
    }

}

?>
